package com.lex.notepad;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lex.dialoglibrary.CustomDialog;
import com.lex.widget.text_view.CustomFontTextView;

/**
 * Created by Administrator on 2016/6/23.
 */
public class RegisterActivity extends Activity {

    public static final String NAME = "name";
    public static final String PASSWORD = "password";

    private String name,password;
    private Dialog dialog;
    private Button zhuce;
    private EditText field_reg_name;  //接收用户名的组件
    private EditText filed_reg_pass;  //接收密码的组件

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        findView();
        initView();
    }

    protected void findView() {
        field_reg_name = (EditText) findViewById(R.id.user_reg_name);  //首先获取用来输入用户名的组件
        filed_reg_pass = (EditText) findViewById(R.id.pass_reg_words); //同时也需要获取输入密码的组件
        zhuce = (Button) findViewById(R.id.zhuce);
        // Restore preferences
    }


    protected void initView() {

        zhuce.setOnClickListener(onClickListener);
        initDialog();
    }

    private void initDialog() {
        dialog = new CustomDialog(RegisterActivity.this, R.layout.dialog_register);
        dialog.findViewById(R.id.confirm_dialog_btn).setOnClickListener(onClickListener);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.zhuce:
                     name = field_reg_name.getText().toString();
                     password = filed_reg_pass.getText().toString();
                    if (name.length() <= 0 && password.length() <= 0) {
                        Toast.makeText(RegisterActivity.this, "用户名或密码为空", Toast.LENGTH_SHORT).show();
                    } else if (name.length() <= 0) {
                        Toast.makeText(RegisterActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                    } else if (password.length() <= 0) {
                        Toast.makeText(RegisterActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                    } else if (name != null && password != null) {

                        SharedPreferences settings = getSharedPreferences(name, MODE_PRIVATE); //首先获取一个SharedPreferences对象
                        settings.edit()
                                .putString(NAME, name)
                                .putString(PASSWORD, password)
                                .commit();
                        Toast.makeText(RegisterActivity.this,"您的用户名是：" + name , Toast.LENGTH_SHORT).show();
                    dialog.show();
                    }
                    break;
                case R.id.confirm_dialog_btn:
                    startActivity(new Intent(RegisterActivity.this, NotePadMainActivity.class));
                    finish();
                    dialog.dismiss();
                    break;
            }

        }
    };
}